package com.onesports.intelligent.k12.polarlight.common.controller;

import cn.oneframework.common.utils.MessageUtils;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.common.utils.FileUtils;
import com.onesports.intelligent.k12.polarlight.enums.DownloadFileNameEnum;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * 通用请求处理
 *
 * @author onesports
 */
@Controller
@RequestMapping("/common")
public class CommonController {
    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @ApiOperation(value = "通用下载请求", notes = "通用下载请求")
    @GetMapping("download")
    public void fileDownload(String fileName, HttpServletResponse response, HttpServletRequest request) {
        // 模板文件名称
        // 下载的文件名称
        String exportFileName = null;
        DownloadFileNameEnum fileNameEnum = DownloadFileNameEnum.getByFileName(fileName);
        if(fileNameEnum == null){
            log.error("文件不存在{}",fileName);
            throw new BusinessException("文件不存在");
        }
        try {
            exportFileName = URLEncoder.encode(fileNameEnum.getExportName(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("出现小问题啦{}",e);
            throw new BusinessException("出现小问题啦");
        }
        BufferedInputStream inputStream = null;
        OutputStream outputStream = null;
        String path = "/template/" +fileNameEnum.getRealFileName();
        InputStream sourFileIn = this.getClass().getResourceAsStream(path);

        try {
            inputStream = new BufferedInputStream(sourFileIn);
            byte[] content = new byte[inputStream.available()];
            inputStream.read(content, 0, content.length);

            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.addHeader("charset", "UTF-8");
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition","attachment;filename=" + new String(exportFileName.getBytes("utf-8"),"UTF-8"));

            outputStream = response.getOutputStream();
            outputStream.write(content);
            outputStream.flush();
        } catch (IOException e) {
            log.error("文件下载失败", e);
            throw new BusinessException("文件下载失败");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}